package com.decent.ejfadmin.finance.dao;

import com.decent.ejfadmin.common.bean.AlipayConfig;
import com.decent.ejfadmin.common.bean.Page;
import com.decent.ejfadmin.common.bean.SupAlipay;
import com.decent.ejfadmin.finance.dao.provider.AlipayConfigDaoProvider;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;

/**
 * 支付宝配置
 *
 * @author weicx
 * @date 2019/02/25
 */
@Repository
@Mapper
public interface AlipayConfigDao {
    /**
     * 查询支付宝配置
     *
     * @return
     */
    @Select("SELECT " +
            "id, " +
            "alipay, " +
            "`status`, " +
            "quota, " +
            "createTime, " +
            "updateTime, " +
            "remark " +
            "FROM " +
            "yuhuang_alipay_config")
    List<AlipayConfig> queryAlipayConfigs();

    /**
     * 查询支付宝当日提卡总额
     *
     * @param alipay 支付宝账号
     * @return
     */
    @Select("SELECT IFNULL(SUM(price),0) FROM `yuhuang_withdraw_record` WHERE payBank=#{alipay} AND to_days(endTime) = to_days(now()) and state!=3")
    BigDecimal getAlipayWithdrawPriceSum(@Param("alipay") String alipay);

    /**
     * 修改限额
     *
     * @param alipayConfig 支付宝配置
     * @return
     */
    @Update("update yuhuang_alipay_config set quota=#{alipayConfig.quota},updateTime=now() where id=#{alipayConfig.id} ")
    int updateAlipayConfig(@Param("alipayConfig") AlipayConfig alipayConfig);

    /**
     * 新增支付宝配置
     *
     * @param alipayConfig 支付宝配置
     * @return
     */
    @Insert("insert into yuhuang_alipay_config (alipay,quota,createTime,updateTime,status) values(#{alipayConfig.alipay},#{alipayConfig.quota},now(),now(),0)")
    int insertAlipayConfig(@Param("alipayConfig") AlipayConfig alipayConfig);

    /**
     * 修改支付宝配置状态
     *
     * @param status 状态
     * @param id     支付宝id
     * @return
     */
    @Update("update yuhuang_alipay_config set status=#{status},updateTime=now() where id=#{id}")
    int updateStatus(@Param("status") Integer status, @Param("id") Integer id);

    /**
     * 查询支付宝
     *
     * @param id 支付宝id
     * @return
     */
    @Select("select alipay from yuhuang_alipay_config where id=#{id}")
    String getOpenAlipay(@Param("id") Integer id);

    /**
     * 关闭所有支付宝
     *
     * @return
     */
    @Update("update yuhuang_alipay_config set status=0")
    int updateAllStatus();

    /**
     * 会员支付宝
     *
     * @param supAlipay 查询实体
     * @return
     */
    @SelectProvider(type = AlipayConfigDaoProvider.class, method = "supAlipayCount")
    int supAlipayCount(@Param("supAlipay") SupAlipay supAlipay);


    /**
     * 会员支付宝
     *
     * @param supAlipay 查询实体
     * @param page      分页实体
     * @return
     */
    @SelectProvider(type = AlipayConfigDaoProvider.class, method = "supAlipayDetail")
    List<SupAlipay> supAlipayDetail(@Param("supAlipay") SupAlipay supAlipay, @Param("page") Page page);

    /**
     * 修改会员支付宝
     *
     * @param supAlipay 会员支付宝
     * @return
     */
    @Update("update yuhuang_sup_alipay set alipay=#{supAlipay.alipay},gmt_modified=now() where id=#{supAlipay.id} ")
    int updateSupAlipay(@Param("supAlipay") SupAlipay supAlipay);

    /**
     * 删除会员支付宝
     *
     * @param id
     * @return
     */
    @Delete("update  yuhuang_sup_alipay set is_use=0 where id=#{id}")
    int updateSupAlipayStatus(@Param("id") Integer id);

}
